<?php


class tx_zlib_viewhelper_feuser extends tx_zlib_abstract_viewhelper
{

	/**
	 * @var tslib_feUserAuth
	 */
	private $_feUser;

	/**
	 * @return tx_zlib_viewhelper_feuser
	 */
	public function feuser()
	{
		$this->_feUser = $GLOBALS['TSFE']->fe_user;
		return $this;
	}

	/**
	 * @return boolean
	 */
	public function loggedIn()
	{
		if($this->_feUser instanceof tslib_feUserAuth) {
			if(is_array($this->_feUser->user)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param string $usergroup Comma separated values
	 * @return boolean
	 */
	public function belongsTo($usergroup = '')
	{
	    if(!$this->loggedIn()) {
	        return false;
	    }
	    $usergroup = trim($usergroup);
	    if(empty($usergroup)) {
	        return false;
	    }
	    if(!empty($usergroup)) {
	        $usergroups = explode(',', $usergroup);
	        foreach($usergroups AS $uid) {
	            if(isset($this->_feUser->groupData['uid'][$uid])) {
	                return true;
	            }
	        }
	    }
	    return false;
	}

	/**
	 * @param string $key
	 * @return mixed
	 */
	public function data($key = null)
	{
		if($this->loggedIn()) {
			if(is_array($this->_feUser->user)) {
				if(is_null($key)) {
					return $this->_feUser->user;
				}
				if(array_key_exists($key, $this->_feUser->user)) {
					return $this->_feUser->user[$key];
				}
			}
		}
		return null;
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_feuser.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_feuser.php']);
}
